package xyz.yiui.service.user;

import org.junit.jupiter.api.Test;
import xyz.yiui.dao.BaseDao;
import xyz.yiui.dao.user.UserDao;
import xyz.yiui.dao.user.UserDaoImpl;
import xyz.yiui.pojo.User;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService{
    ///引入dao层
    private UserDao userDao;
    public UserServiceImpl(){
        userDao=new UserDaoImpl();
    }
    @Override
    public User login(String userCode, String password) {
        Connection connection=null;
        User user=null;
        try{
            connection= BaseDao.getConnection();
            ///调用dao层具体操作
            user= userDao.getLoginUser(connection,userCode);
            if(!password.equals(user.getUserPassword())) user=null;
        }catch (SQLException | ClassNotFoundException e){
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return user;///返回查询结果
    }
    ///修改用户密码
    @Override
    public boolean updatePwd(int id, String password) {
        boolean flag=false;
        Connection connection=null;
        try{
            connection=BaseDao.getConnection();
            if(userDao.updatePwd(connection,id,password)>0){
                flag=true;
            }
        }catch (SQLException | ClassNotFoundException e){
            e.printStackTrace();
        }
        finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    //查询记录数
    @Override
    public int getUserCount(String username, int userRole) {
        Connection connection=null;
        int cnt=0;
        try{
            connection=BaseDao.getConnection();
            cnt=userDao.getUserCount(connection,username,userRole);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection,null,null);
        }
        return cnt;
    }
    //得到用户列表
    @Override
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<User> userList = null;
        try {
            connection = BaseDao.getConnection();
            userList = userDao.getUserList(connection, queryUserName,queryUserRole,currentPageNo,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return userList;
    }
    //增加用户
    @Override
    public boolean add(User user) {
        boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);//开启JDBC事务管理
            int updateRows = userDao.add(connection,user);
            connection.commit();//成功的话直接提交
            if(updateRows > 0){
                flag = true;
                System.out.println("add success!");
            }else{
                System.out.println("add failed!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("rollback==================");
                connection.rollback();//失败的话回滚
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }finally{
            //关闭资源
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }
    ///删除用户
    @Override
    public boolean deleteUserById(Integer delId) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);//开启JDBC事务管理
            connection.commit();//成功的话直接提交
            if(userDao.deleteUserById(connection,delId) > 0)
                flag = true;
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("rollback==================");
                connection.rollback();//失败的话回滚
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }
    //根据id得到用户
    @Override
    public User getUserById(String id) {
        User user = null;
        Connection connection = null;
        try{
            connection = BaseDao.getConnection();
            user = userDao.getUserById(connection,id);
        }catch (Exception e) {
            e.printStackTrace();
            user = null;
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }
    //更新用户
    @Override
    public boolean modify(User user) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            if(userDao.modify(connection,user) > 0)
                flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    //根据用户编码查找用户
    @Override
    public User selectUserCodeExist(String userCode) {
        Connection connection = null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getUserByCode(connection, userCode);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }



    @Test
    public void test_updatePwd(){
        UserServiceImpl userService=new UserServiceImpl();
        User admin=userService.login("admin","111");
        System.out.println(admin.getUserPassword());
    }
    @Test
    public void test_getUserCount(){
        UserServiceImpl userService=new UserServiceImpl();
        int ans=userService.getUserCount(null,1);
        System.out.println(ans);
    }

    @Test
    public void test_getUserList(){
        UserServiceImpl userService=new UserServiceImpl();
        List<User> ans=userService.getUserList(null,3,2,5);
        for(User t:ans) System.out.println(t);
    }

}
